// Copyright Epic Games, Inc. All Rights Reserved.

#include "GKConfigEditor.h"
#include "GKConfigEditorStyle.h"
#include "GKConfigSetting.h"
#include "ISettingsModule.h"
#include "ISettingsSection.h"
#include "LevelEditor.h"
#include "WorkspaceMenuStructure.h"
#include "WorkspaceMenuStructureModule.h"
#include "Misc/MessageDialog.h"
#include "Slate/SGKConfigTab.h"

#define LOCTEXT_NAMESPACE "FGKConfigEditorModule"

void FGKConfigEditorModule::StartupModule()
{
	FGKConfigEditorStyle::Initialize();
	FGKConfigEditorStyle::ReloadTextures();

	FString ConfigPath = FPaths::ProjectPluginsDir()/TEXT("GKConfig/Config/GKConfig.ini");
	// FConfigCacheIni::LoadGlobalIniFile(ConfigPath,TEXT("GKConfig"), NULL, NULL, true,true,*ConfigPath);
	
	FGlobalTabmanager::Get()->RegisterNomadTabSpawner(
		FName(TEXT("GKConfigPlugin")),
		FOnSpawnTab::CreateRaw(this, &FGKConfigEditorModule::SpawnGKConfigEditorTab))
			.SetDisplayName(NSLOCTEXT("FGKConfigEditorModule", "GKConfig", "GKConfig"))
			.SetTooltipText(NSLOCTEXT("FGKConfigEditorModule", "GKConfigTooltipText", "Open GKConfig Editor tab."))
			.SetGroup(WorkspaceMenu::GetMenuStructure().GetLevelEditorCategory())
			.SetIcon(FSlateIcon(FGKConfigEditorStyle::GetStyleSetName(), "GKConfigEditor.OpenPluginWindow")
		);

	ISettingsModule* SettingModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings");
	if (SettingModule)
	{
		ISettingsSectionPtr SettingsSection = SettingModule->RegisterSettings("Project", "CustomSettings", "GKConfig",
			NSLOCTEXT("GameplayAbilitiesEditorModule", "GKConfig", "GKConfig"),
			NSLOCTEXT("GameplayAbilitiesEditorModule", "GKConfig", "GKConfig Test"),
			GetMutableDefault<UGKConfigSetting>()
			);
		if (SettingsSection.IsValid())
		{
			SettingsSection->OnModified().BindRaw(this, &FGKConfigEditorModule::HandleSettingsSaved);
		}
	}
}

void FGKConfigEditorModule::ShutdownModule()
{
	FGlobalTabmanager::Get()->UnregisterNomadTabSpawner(FName(TEXT("GKConfigPlugin")));
	ISettingsModule* SettingModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings");
	if (SettingModule)
	{
		SettingModule->UnregisterSettings("Project", "CustomSettings", "GKConfig");
		if (GKConfigEditorSlate.IsValid())
		{
			GKConfigEditorSlate.Pin()->RequestCloseTab();
		}
	}
	FGKConfigEditorStyle::Shutdown();
}

TSharedRef<SDockTab> FGKConfigEditorModule::SpawnGKConfigEditorTab(const FSpawnTabArgs& Args)
{
	return SAssignNew(GKConfigEditorSlate, SDockTab)
		.TabRole(ETabRole::NomadTab)
		[
			SpawnGKConfigEditorUI().ToSharedRef()
		]
	;
}

TSharedPtr<SWidget> FGKConfigEditorModule::SpawnGKConfigEditorUI()
{
	TSharedPtr<SWidget> Result = SNullWidget::NullWidget;
	if(IsInGameThread())
	{
		TSharedPtr<SGKConfigTab> TempResult = SNew(SGKConfigTab);
		Result = TempResult;
		GKConfigTab = TempResult;
	}
	return Result;
}

bool FGKConfigEditorModule::HandleSettingsSaved()
{
	// UGKConfigSetting* Settings = GetMutableDefault<UGKConfigSetting>();
	// Settings->SaveConfig();
	// bool ResaveSettings = false;
	// // You can put any validation code in here and resave the settings in case an invalid
	// // value has been entered
	// if (ResaveSettings)
	// {
	// 	
	// }

	return true;
}

#undef LOCTEXT_NAMESPACE

IMPLEMENT_MODULE(FGKConfigEditorModule, GKConfigEditor)
